﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Web;
using core;
using shop;

namespace comix
{
    public class Book:SiteItemSingle,ISiteItemMember,ISellable
    {
        private string m_title;

        public string Title
        {
            get { return m_title; }
            set { m_title = value; }
        }
        private string m_description;

        public string Description
        {
            get { return m_description; }
            set { m_description = value; }
        }
        private int m_npages;

        public int Npages
        {
            get { return m_npages; }
            set { m_npages = value; }
        }
        private float m_rating;

        public float Rating
        {
            get { return m_rating; }
            set { m_rating = value; }
        }
        protected DateTime m_date;
        protected DateTime m_originalrelease;
        protected int m_sales;
        protected int m_nratings;
        protected bool m_hasdemo;
        protected int m_ser_id;
        private Series m_series;
        private bool m_owned;

        public bool Owned
        {
            get { return m_owned; }
            set { m_owned = value; }
        }

        private bool m_demoowned;

        public bool DemoOwned
        {
            get { return m_demoowned; }
            set { m_demoowned = value; }
        }


        private AuthorsCollection m_authorsOfBook;
        public AuthorsCollection AuthorsOfBook
        {
            get
            {
                if (m_authorsOfBook == null)
                {
                    m_authorsOfBook = new AuthorsCollection();
                    m_authorsOfBook.Command.CommandText= "SELECT Authors.* FROM Authors INNER JOIN BOOKS2AUTHORS on aut_id = a2b_aut_id WHERE a2b_bok_id = @bok_id AND a2b_is_writter = 1 AND a2b_deleted = 0";
                    m_authorsOfBook.Command.Parameters.Add(new SqlParameter("@bok_id", Id));
                    m_authorsOfBook.FillItem();
                }
                return m_authorsOfBook;
            }
        }

        private AuthorsCollection m_designersOfBook;
        public AuthorsCollection DesignersOfBook
        {
            get
            {
                if (m_designersOfBook == null)
                {
                    m_designersOfBook = new AuthorsCollection();
                    m_designersOfBook.Command.CommandText = "SELECT Authors.* FROM Authors INNER JOIN BOOKS2AUTHORS on aut_id = a2b_aut_id WHERE a2b_bok_id = @bok_id AND a2b_is_designer = 1 AND a2b_deleted = 0";
                    m_designersOfBook.Command.Parameters.Add(new SqlParameter("@bok_id", Id));
                    m_designersOfBook.FillItem();
                }
                return m_designersOfBook;
            }
        }

        public Series Series
        {
            get
            {
                if(m_series == null)
                {
                    m_series = new Series();
                    m_series.Id = m_ser_id;
                    m_series.FillItem();
                }
                return m_series;
            }
        }

        private decimal m_price;

        public decimal Price
        {
            get { return m_price; }
            set { m_price = value; }
        }
       

        public Book()
        {
            LCustomer cst = (LCustomer)HttpContext.Current.Session["customer"];
            if(cst.Id==-1)
                this.Command.CommandText = "SELECT Books.*,ptr_total,0 as bok_owned, 0 as bok_demoowned FROM BOOKS INNER JOIN TIERS on bok_ptr_id = ptr_id WHERE bok_id = @bok_id";
            else
                Command.CommandText = "SELECT Books.*,ptr_total, CASE (SELECT COUNT(*) FROM BOOKS2USERS WHERE u2b_bok_id = @bok_id AND u2b_usr_id = " + cst.Id.ToString() + " AND u2b_issample = 0) WHEN 0 THEN 0 ELSE 1 END as bok_owned, CASE (SELECT COUNT(*) FROM BOOKS2USERS WHERE u2b_bok_id = bok_id AND u2b_usr_id = " + cst.Id.ToString() + " AND u2b_issample = 1) WHEN 0 THEN 0 ELSE 1 END as bok_demoowned  FROM BOOKS INNER JOIN TIERS on bok_ptr_id = ptr_id WHERE bok_id = @bok_id";
        }

        public override void DoFill(System.Data.SqlClient.SqlDataReader rdr)
        {
            m_id = (int)rdr["bok_id"];
            m_title = (string)rdr["bok_title"];
            m_description = (string)rdr["bok_description"];
            m_npages = (int)rdr["bok_pages"];
            m_rating = Convert.ToSingle(rdr["bok_rating"]);
            m_date = (DateTime)rdr["bok_date"];
            m_originalrelease = (DateTime)rdr["bok_originalrelease"];
            m_sales = (int)rdr["bok_sales"];
            m_nratings = (int)rdr["bok_nratings"];
            m_hasdemo = (bool)rdr["bok_hasdemo"];
            m_ser_id = (int)rdr["bok_ser_id"];
            m_price = (decimal)rdr["ptr_total"];
            m_owned = (rdr["bok_owned"].ToString()=="1");
            m_demoowned = (rdr["bok_demoowned"].ToString() == "1");
        }

        public override void SetSelectParameters()
        {
            m_com.Parameters.Clear();
            m_com.Parameters.Add(new SqlParameter("@bok_id", m_id));
        }

        #region ISiteItemMember Members

        public void FillMember(System.Data.SqlClient.SqlDataReader rdr)
        {
            m_id = (int)rdr["bok_id"];
            m_title = (string)rdr["bok_title"];
            m_hasdemo = (bool)rdr["bok_hasdemo"];
            m_price = (decimal)rdr["ptr_total"];
            m_description = (string)rdr["bok_description"];
            m_rating = Convert.ToSingle(rdr["bok_rating"]);
            m_owned = (rdr["bok_owned"].ToString() == "1");
        }

        public string SmallThumbnailPath()
        {
            return "http://www.lcomix.com/thumbs/small/" + m_id.ToString() + ".jpg";
        }

        public string LargeThumbnailPath()
        {
            return "http://www.lcomix.com/thumbs/large/" + m_id.ToString() + ".jpg";
        }

        #endregion

        #region ISellable Members

        public void AddToTransactionContents(shop.cart.CartItem ci, shop.Transactions.TransactionContentsCollection tccollection)
        {
            throw new NotImplementedException();
        }

        public string CartName()
        {
            return m_title;
        }

        public bool IsSimple()
        {
            return true;
        }

        public int ItemId()
        {
            return m_id;
        }

        public shop.cart.Cart ToSubCart()
        {
            throw new NotImplementedException();
        }

        public float UnitPrice()
        {
            return Convert.ToSingle(m_price);
        }

        #endregion
    }
}
